package org.ielts.repository;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.ielts.domain.base.Classroom;
import org.ielts.domain.base.CourseType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.yifeng.dao.HibernateRepository;

@Repository
public class ClassroomRepository extends HibernateRepository<Classroom, String> {

	public Classroom findByNameAndCampus(String name, String campus) {
		DetachedCriteria detachedCriteria = createDetachedCriteria();
		detachedCriteria.add(Restrictions.eq("name", name)).createAlias("campus", "campus")
				.add(Restrictions.eq("campus.id", campus));

		Classroom classroom = uniqueResult(detachedCriteria);

		return classroom;
	}

	public Iterable<Classroom> findAllByCampus(String campusId) {
		DetachedCriteria detachedCriteria = createDetachedCriteria();

		detachedCriteria.createAlias("campus", "campus").add(Restrictions.eq("campus.id", campusId))
				.addOrder(Order.asc("name"));

		@SuppressWarnings("unchecked")
		Iterable<Classroom> classrooms = hibernateTemplate.findByCriteria(detachedCriteria);

		return classrooms;
	}
}
